package org.fluidripple.socket.outbound;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import lombok.extern.slf4j.Slf4j;

/**
 * 出站处理器
 */

@Slf4j
@ChannelHandler.Sharable
public class ProtocolEncoderHandler extends ChannelOutboundHandlerAdapter {

    public static final String NAME="protocol-encoder";

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise){
        if (ByteBuf.class.isAssignableFrom(msg.getClass())){
            ByteBuf buf = (ByteBuf) msg;
            log.info("发送数据包=> {}", ByteBufUtil.hexDump(buf));
            ctx.writeAndFlush(msg);
        }
    }
}
